<?php
class OrderAction extends Action{
	public function check(){
		if(!isset($_SESSION['euname'])) {
			$this->islogin="1";
		}else{
			$member=$_SESSION['euname'];
			$Member=M('Member');
			$Addr=M('Member_addr');
			$Coupons=M('Coupons');
			$map['uname']=$member;
			$mid=$Member->where($map)->find();
			$this->addr=$Addr->where('member_id='.$mid['member_id'])->select();
			$map2['owner']=$mid['member_id'];
			$map2['type']='online';
			$map2['status']='active';
			$map2['min_money']=array('elt',$_SESSION['total_money']);
			$map2['begin_time']=array('elt',time());
			$map2['end_time']=array('egt',time());
			$this->coupons=$Coupons->where($map2)->select();
			$this->member=$mid;
		}
        
		$Time=M('Dlytime');
		$this->dlytime=$Time->select();
		$this->cart=R('Cart/get_cart');
		$this->ship=R('Ship/ship_choose');
		$this->pay=R('Pay/payment');
		$this->total_money=$_SESSION['total_money'];
		$this->total_weight=$_SESSION['total_weight'];
		$this->display();
	}

	//创建订单
	public function create_order(){
		$Log=M('Order_log');
		//会员信息
		$member=R('Member/get_user');
		$order['member_id']=$member['member_id'];//@
		$order['user_ip']=get_client_ip();
		//收货信息
		$addr_id=$this->_post('old_addr');//获取已经存在的地址id
		$ship_province=$this->_post('province');//获取新的省份id
		$ship_city        =$this->_post('city');//获取新的市id
		$ship_zone      =$this->_post('zone');//获取新的县@名
		$ship_street     =$this->_post('street');
		$ship_name     =$this->_post('ship_name');//获取新的地址姓名
		$ship_mobile    =$this->_post('ship_mobile');
		$order['ship_time']=$this->_post('dlytime');
		$order['leave_msg']=$this->_post('cus_msg');//@
		$order['invoice_title']=$this->_post('invoice');
		//处理收货信息
		$Address=M('Member_addr');
		if($addr_id=='no'){//使用的是新地址
			//获取省份、市
			$Area=M('Area');
			$pro=$Area->where('area_id='.$ship_province)->find();
			$ci=$Area->where('area_id='.$ship_city)->find();
			$province=$pro['local_name'];
			$city=$ci['local_name'];
			$addr_data['member_id']=$member['member_id'];
			$addr_data['name']=$ship_name;
			$addr_data['province']=$province;
			$addr_data['address']=$province.$city.$ship_zone.$ship_street;
			$addr_data['mobile']=$ship_mobile;
			$Address->add($addr_data);//保存新地址
			$order['ship_name']=$ship_name;             //@
			$order['ship_addr']=$addr_data['address'];//@
			$order['ship_mobile']=$ship_mobile;          //@
		}else{ //使用以前地址
			$old_addr=$Address->where('addr_id='.$addr_id)->find();
			$order['ship_name']=$old_addr['name'];     //@
			$order['ship_addr']=$old_addr['address'];  //@
			$order['ship_mobile']=$old_addr['mobile']; //@
		}

		//生成订单号
		$order_id=date('YmdH').rand(10000,99999);
		$order['order_id']=$order_id;
		$order['create_time']=time();//@
		$order['last_time']=$order['create_time'];    //@

		//配送方式
		$ship_id=$this->_post('shipping');
		$Ship=M('Express');
		$ship=$Ship->where('id='.$ship_id)->find();
		$order['is_delivery']=$ship['is_delivery'];//@
		$order['shipping_id']=$ship_id;//@
		$order['shipping']=$ship['ex_name'];//@

		//支付方式
		$order['payment']=$this->_post('pay'); //@
		//判断支付类型：在线支付、货到付款
		$Payment=M('Payment');
		$payment=$Payment->where('id='.$order['payment'])->find();
		//返回支付类型，根据是否为在线支付跳转到相应的界面！！
		if($payment['type']=='online'){
			$order['bank']=$this->_post('bank');
			$order['is_onlinpay']='true';
		}else{
			$order['is_onlinpay']='false';
		}

		//商品优惠
		$Coupons=M('Coupons');
		$coumap['passcode']=$this->_post('cou_id');
		$coumap2['passcode']=$this->_post('cou_id');
		$coumap['status']='active';
		$coupons=$Coupons->where($coumap)->find();

		//金额信息
		$order['total_money']=$_SESSION['total_money'];
		$order['dly_money']=$this->_post('dly_price');
		$total_price=$this->_post('total_price');
		if(empty($total_price)){
			$total_price=0;
		}
        
		$zongjia=$order['total_money']+$order['dly_money'];

		if($coupons){
			/*总价小于优惠券额度则优惠金额为总价，支付完成；大于则优惠券金额为其额度*/
			if($zongjia<$coupons['cou_amount']){
				$order['pmt_money']=$zongjia;
				$order['payed']=0;
				$order['pay_status']=1;
			}else{
				$order['pmt_money']=$coupons['cou_amount'];
			}
		    $order['pmt_name']=$coumap['passcode'];
		}else{
			$order['pmt_money']=0;
		}

		$order['final_money']=$zongjia-$order['pmt_money'];//优惠后需支付金额
		
		//if($order['final_money']!=$total_price){$this->redirect('Order/check');}

		//礼品卡支付
		$lp_price=$this->_post('lp_price');
		if($lp_price>0){
			if($member['wallet']<$order['final_money']){
				$order['payed']=$member['wallet'];//账户余额不足则全部支付
			}else{
				$order['payed']=$order['final_money'];//账户余额充足则支付完成
				$order['pay_status']=1;
			}
		}else{
			$order['payed']=0;
		}

		$_SESSION['final_money']=$order['final_money']-$order['payed'];

		//商品信息
		$cart=R('Cart/get_cart');
		$order['weight']=$_SESSION['total_weight'];//@
		$order['item_num']=count($cart);

		$Order=M('Orders');
		$order_re=$Order->add($order);    //插入订单

		//失败处理
		if(false==$order_re){ 
			$order['order_id']=date('YmdH').rand(10000,99999);
			$order_re2=$Order->add($order);    //再次尝试插入订单
			if(false==$order_re2){$this->redirect('Order/check');}
		} 
		
		$Item=M('Orders_item');
		$data['order_id']=$order_id;
		$tostr = array();
		$Product = M('Products');
		$Goods = M('Goods');
		for($i=0;$i<count($cart);$i++){
			$data['product_id']=$cart[$i]['product_id'];
			$data['goods_id']=$cart[$i]['goods_id'];
			$data['name']=$cart[$i]['name'];
			$data['price']=$cart[$i]['price'];
			$data['num']=$cart[$i]['product_num'];
			$data['amount']=$cart[$i]['s_money'];
			$data['props']=$cart[$i]['props'];
			$data['bn']=$cart[$i]['bn'];
			$item_re=$Item->add($data);   //插入订单明细
			$Product->where('product_id='.$data['product_id'])->setDec('store',$data['num']);//减库存
			$Goods->where('goods_id='.$data['goods_id'])->setInc('buy_count',$data['num']);//加购买量
			$tostr[] = $cart[$i]['name'];
		}
        $data2['tostr']=implode("+",$tostr);
		/*
		if($order['final_money']==0){
			$data2['pay_status']=1;
		}
		*/
		$Order->where('order_id='.$order_id)->save($data2);
		

		//礼品卡支付结果处理
		$Mwallet=M('Member_wallet');
		$wlog['member_id']=$member['member_id'];
		$wlog['action']='0';
		$wlog['money']=$order['payed'];
		$wlog['op_time']=time();
		$wlog['op_name']=$member['uname'];
		$wlog['order_id']=$order_id;

		if($order_re&&$item_re){
			//优惠券失效
			$coudata['status']='used';
			$coudata['owner']=$order['member_id'];
			$coudata['order_id']=$order['order_id'];
			$coudata['use_time']=time();
			$Coupons->where($coumap2)->save($coudata);
			
			//用户礼品卡余额扣除
			$Member=M('Member');
			$Member->where('member_id='.$member['member_id'])->setDec('wallet',$order['payed']);
			$wlog['result']='t';
            
			if($order['payed']>0){
				//支付记录
				$Pay=M('Order_pay');
				$paylog['order_id']=$order_id;
				$paylog['payed']=$order['payed'];
				$paylog['payment']=4;
				$paylog['pay_time']=time();
				$paylog['pay_id']=$member['member_id'];
				$paylog['result']='success';
				$Pay->add($paylog);

				$log['order_id']=$order_id;
				$log['op_id']=$member['member_id'];
				$log['op_name']=$member['uname'];
				$log['log_text']="订单：".$order_id."创建";
				$log['acttime']=time();
				$log['behavior']='订单创建';
				$log['result']='success';
				$Log->add($log);

				$log2['order_id']=$order_id;
				$log2['op_id']=$member['member_id'];
				$log2['op_name']=$member['uname'];
				$log2['log_text']="订单：".$order_id."使用礼品卡余额支付：".$order['payed'];
				$log2['acttime']=time();
				$log2['behavior']='订单付款';
				$log2['result']='success';
				$Log->add($log2);
			}
			$return=array('order_id'=>$order_id,'result'=>"success",'paytype'=>$payment['type'],'final_money'=>$order['final_money']);
			R('Cart/clear_cart');
			unset($_SESSION['total_weight']);
		}else{
			$wlog['result']='f';
			$Order->where('order_id='.$order_id)->delete();
			$Item->where('order_id='.$order_id)->delete();
			$return=array('result'=>"faile");
		}
		//保存礼品卡支付记录
		if($order['payed']>0){
			$Mwallet->add($wlog);
		}
        
		//返回结果
		echo json_encode($return);

	  //邮件通知
	  if(C('EMAIL_CREATE_ORDER')=='1'){
		    R("Public/smail",array($member['email'],$member['uname'],'订单提交成功',$order_id,'order'));
	  }
	}
	/*-订单创建end-*/

	//计算运费
	public function get_ship_money(){
		$Express=M('Express ');
		$Ship=M('Express_value');
		$pid=$this->_post('province');
		$old_addr=$this->_post('addr');
		$id=$this->_post('ship');
        
		if($old_addr=='no'&&!empty($pid)){
		   $map['ex_area']=$this->get_area($pid);
		   $ship=$Ship->where($map)->find();
		}else{
			$map['ex_area']=$this->get_addr($old_addr);
		   $ship=$Ship->where($map)->find();
		}
		$ex=$Express->where('id='.$id)->find();

		if($ex['type']=='2'&&$ship==true){
			$fee=array("one_w"=>$ex['one_w'],"two_w"=>$ex['two_w'],"onefee"=>$ship['onefee'],"twofee"=>$ship['twofee']);
		}else{
			$fee=array("one_w"=>$ex['one_w'],"two_w"=>$ex['two_w'],"onefee"=>$ex['onefee'],"twofee"=>$ex['twofee']);
		}
		echo json_encode($fee);
	}
	
	//计算优惠
	public function get_coupons(){
		$Coupons=M('Coupons');
		$map['passcode']=$this->_post('code');
		if($coupons=$Coupons->where($map)->find()){
		    echo $coupons['cou_amount'];
		}else{
			echo 0;
		}
	}

	//实体优惠券验证
	public function ck_coupons(){
		$Coupons=M('Coupons');
		$map['owner']=0;
		$map['passcode']=$this->_post('code');
		$map['type']='offline';
		$map['status']='active';
		$map['min_money']=array('elt',$_SESSION['total_money']);
		$map['begin_time']=array('elt',time());
		$map['end_time']=array('egt',time());
		$cou=$Coupons->where($map)->find();
		if($cou){
			$return=array('result'=>'true','code'=>$cou['passcode'],'money'=>$cou['cou_amount']);
		}else{
			$return=array('result'=>'false');
		}
		echo json_encode($return);
	}

	//得到地区
	public function get_area($pid){
		$Area=M('Area');
		$area=$Area->where('area_id='.$pid)->find();
		return $area['local_name'];
	}
	//获取已经存在的地址
	public function get_addr($id){
		$Addr=M('Member_addr');
		$addr=$Addr->where('addr_id='.$id)->find();
		return $addr['province'];
	}


}

?>